Ext.define('SKJF.controller.MSTCURTENGAH',{
	extend: 'Ext.app.Controller',
	views: ['MASTER.v_mstcurtengah','MASTER.v_mstcurtengah_form'],
	models: ['m_mstcurtengah'],
	stores: ['s_mstcurtengah'],
	
	requires: ['Ext.ModelManager'],
	
	refs: [{
		ref: 'Listmstcurtengah',
		selector: 'Listmstcurtengah'
	}, {
		ref: 'v_mstcurtengah_form',
		selector: 'v_mstcurtengah_form'
	}, {
		ref: 'Savemstcurtengah',
		selector: 'v_mstcurtengah_form #save'
	}, {
		ref: 'Createmstcurtengah',
		selector: 'v_mstcurtengah_form #create'
	}, {
		ref: 'MSTCURTENGAH',
		selector: 'MSTCURTENGAH'
	}],


	init: function(){
		this.control({
			'MSTCURTENGAH': {
				'afterrender': this.mstcurtengahAfterRender
			},
			'v_mstcurtengah_form': {
				'afterlayout': this.mstcurtengahAfterLayout
			},
			'Listmstcurtengah': {
				'selectionchange': this.enableDelete,
				'itemdblclick': this.updateListmstcurtengah
			},
			'Listmstcurtengah button[action=create]': {
				click: this.createRecord
			},
			'Listmstcurtengah button[action=delete]': {
				click: this.deleteRecord
			},
			'Listmstcurtengah button[action=print]': {
				click: this.printRecords
			},
			'v_mstcurtengah_form button[action=save]': {
				click: this.saveV_mstcurtengah_form
			},
			'v_mstcurtengah_form button[action=create]': {
				click: this.saveV_mstcurtengah_form
			},
			'v_mstcurtengah_form button[action=cancel]': {
				click: this.cancelV_mstcurtengah_form
			}
		});
	},
	
	mstcurtengahAfterRender: function(){
		var mstcurtengahStore = this.getListmstcurtengah().getStore();
		mstcurtengahStore.load();
	},
	
	mstcurtengahAfterLayout: function(){this.getV_mstcurtengah_form().down('#TGL_KURS_field').focus(false, true);this.getV_mstcurtengah_form().down('#MATA_UANG_field').focus(false, true);
	},
	
	createRecord: function(){
		var getListmstcurtengah	= this.getListmstcurtengah();
		var getV_mstcurtengah_form= this.getV_mstcurtengah_form(),
			form			= getV_mstcurtengah_form.getForm();
		var getSavemstcurtengah	= this.getSavemstcurtengah();
		var getCreatemstcurtengah	= this.getCreatemstcurtengah();
		
		/* grid-panel */
		getListmstcurtengah.setDisabled(true);
        
		/* form-panel */
		form.reset();
		getV_mstcurtengah_form.down('#TGL_KURS_field').setReadOnly(false);getV_mstcurtengah_form.down('#MATA_UANG_field').setReadOnly(false);
		getSavemstcurtengah.setDisabled(true);
		getCreatemstcurtengah.setDisabled(false);
		getV_mstcurtengah_form.setDisabled(false);
		
		this.getMSTCURTENGAH().setActiveTab(getV_mstcurtengah_form);		
	},
	
	enableDelete: function(dataview, selections){
		this.getListmstcurtengah().down('#btndelete').setDisabled(!selections.length);
	},
	
	updateListmstcurtengah: function(me, record, item, index, e){
		var getMSTCURTENGAH		= this.getMSTCURTENGAH();
		var getListmstcurtengah	= this.getListmstcurtengah();
		var getV_mstcurtengah_form= this.getV_mstcurtengah_form(),
			form			= getV_mstcurtengah_form.getForm();
		var getSavemstcurtengah	= this.getSavemstcurtengah();
		var getCreatemstcurtengah	= this.getCreatemstcurtengah();
		
		getSavemstcurtengah.setDisabled(false);
		getCreatemstcurtengah.setDisabled(true);
		getV_mstcurtengah_form.down('#TGL_KURS_field').setReadOnly(true);getV_mstcurtengah_form.down('#MATA_UANG_field').setReadOnly(true);		
		getV_mstcurtengah_form.loadRecord(record);
		
		getListmstcurtengah.setDisabled(true);
		getV_mstcurtengah_form.setDisabled(false);
		getMSTCURTENGAH.setActiveTab(getV_mstcurtengah_form);
	},
	
	deleteRecord: function(dataview, selections){
		var getstore = this.getListmstcurtengah().getStore();
		var selection = this.getListmstcurtengah().getSelectionModel().getSelection()[0];
		if(selection){
			Ext.Msg.confirm('Confirmation', 'Are you sure to delete this data: "TGL_KURS" = "'+selection.data.TGL_KURS+'","MATA_UANG" = "'+selection.data.MATA_UANG+'","ID" = "'+selection.data.ID+'"?', function(btn){
				if (btn == 'yes'){
					getstore.remove(selection);
					getstore.sync();
				}
			});
			
		}
	},
	
	printRecords: function(){
		var getstore = this.getListmstcurtengah().getStore();
		var jsonData = Ext.encode(Ext.pluck(getstore.data.items, 'data'));
		
		Ext.Ajax.request({
			method: 'POST',
			url: 'c_mstcurtengah/printRecords',
			params: {data: jsonData},
			success: function(response){
				var result=eval(response.responseText);
				switch(result){
				case 1:
					win = window.open('./temp/mstcurtengah.html','mstcurtengah_list','height=400,width=900,resizable=1,scrollbars=1, menubar=1');
					break;
				default:
					Ext.MessageBox.show({
						title: 'Warning',
						msg: 'Unable to print the grid!',
						buttons: Ext.MessageBox.OK,
						animEl: 'save',
						icon: Ext.MessageBox.WARNING
					});
					break;
				}  
			}
		});
	},
	
	saveV_mstcurtengah_form: function(){
		var getMSTCURTENGAH		= this.getMSTCURTENGAH();
		var getListmstcurtengah 	= this.getListmstcurtengah();
		var getV_mstcurtengah_form= this.getV_mstcurtengah_form(),
			form			= getV_mstcurtengah_form.getForm(),
			values			= getV_mstcurtengah_form.getValues();
		var store 			= this.getStore('s_mstcurtengah');
		
		if (form.isValid()) {
			var jsonData = Ext.encode(values);
			
			Ext.Ajax.request({
				method: 'POST',
				url: 'c_mstcurtengah/save',
				params: {data: jsonData},
				success: function(response){
					store.reload({
						callback: function(){
							var newRecordIndex = store.findBy(
								function(record, id) {
									if (record.get('TGL_KURS') === values.TGL_KURS && record.get('MATA_UANG') === values.MATA_UANG)  {
										return true;
									}
									return false;
								}
							);
							/* getListmstcurtengah.getView().select(recordIndex); */
							getListmstcurtengah.getSelectionModel().select(newRecordIndex);
						}
					});
					
					getV_mstcurtengah_form.setDisabled(true);
					getListmstcurtengah.setDisabled(false);
					getMSTCURTENGAH.setActiveTab(getListmstcurtengah);
				}
			});
		}
	},
	
	createV_mstcurtengah_form: function(){
		var getMSTCURTENGAH		= this.getMSTCURTENGAH();
		var getListmstcurtengah 	= this.getListmstcurtengah();
		var getV_mstcurtengah_form= this.getV_mstcurtengah_form(),
			form			= getV_mstcurtengah_form.getForm(),
			values			= getV_mstcurtengah_form.getValues();
		var store 			= this.getStore('s_mstcurtengah');
		
		if (form.isValid()) {
			var jsonData = Ext.encode(values);
			
			Ext.Ajax.request({
				method: 'POST',
				url: 'c_mstcurtengah/save',
				params: {data: jsonData},
				success: function(response){
					store.reload();
					
					getV_mstcurtengah_form.setDisabled(true);
					getListmstcurtengah.setDisabled(false);
					getMSTCURTENGAH.setActiveTab(getListmstcurtengah);
				}
			});
		}
	},
	
	cancelV_mstcurtengah_form: function(){
		var getMSTCURTENGAH		= this.getMSTCURTENGAH();
		var getListmstcurtengah	= this.getListmstcurtengah();
		var getV_mstcurtengah_form= this.getV_mstcurtengah_form(),
			form			= getV_mstcurtengah_form.getForm();
			
		form.reset();
		getV_mstcurtengah_form.setDisabled(true);
		getListmstcurtengah.setDisabled(false);
		getMSTCURTENGAH.setActiveTab(getListmstcurtengah);
	}
	
});